package com.cqhfan.common.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqhfan.common.constants.SystemConstants;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Data;


/**
 * @author 眭传洪
 * @create 2023/12/24 1:17
 */
@Data
public class PageQuery {

    @Min(value = 1, message = "页码不能小于1")
    private Integer pageNum = SystemConstants.DEFAULT_PAGE_NUM;

    @Max(value = 50)
    @Min(value = 1, message = "每页记录数不能小于1")
    private Integer pageSize = SystemConstants.DEFAULT_PAGE_SIZE;

    private boolean isAsc;

    private String sortFiled;

    public <T> Page<T> toMpPage(OrderItem... orderItems) {
        Page<T> page = new Page<>(pageNum, pageSize);
        // 添加排序
        if (orderItems != null && orderItems.length > 0) {
            page.addOrder(orderItems);
        }
        // 添加前端的排序字段
        if (StrUtil.isNotBlank(sortFiled)) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(isAsc);
            orderItem.setColumn(sortFiled);
            page.addOrder(orderItem);
        }
        return page;
    }

    /**
     * 添加默认排序（按照创建时间）
     */
    public <T> Page<T> toMpPageByCreateTime() {
        Page<T> page = new Page<>(pageNum, pageSize);
        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(false);
        orderItem.setColumn(SystemConstants.DEFAULT_SORT_FIELD);
        return page;
    }

}
